package com.jzo2o.foundations.handle;

import com.baomidou.mybatisplus.extension.service.IService;
import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class SpringCacheSyncHandler {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private IServeService serveService;

    @XxlJob("activeRegionCacheSync")
    public void activeRegionCacheSync(){
        //删除缓存
        redisTemplate.delete(RedisConstants.CacheName.JZ_CACHE+"::ACTIVE_REGIONS");
        //添加缓存
        List<RegionSimpleResDTO> regionInfos = regionService.queryActiveRegionListCache();

        for (RegionSimpleResDTO regionInfo : regionInfos) {
            //依次删除
            redisTemplate.delete(RedisConstants.CacheName.SERVE_ICON+"::"+regionInfo.getId());
            redisTemplate.delete(RedisConstants.CacheName.HOT_SERVE+"::"+regionInfo.getId());
            //添加
            serveService.firstPageServeList(regionInfo.getId());
            serveService.hotServeList(regionInfo.getId());
        }
    }
}
